package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.SceneModeMapper;
import com.c611.smartclassroom.model.SceneMode;
import com.c611.smartclassroom.model.SceneModeExample;
import com.c611.smartclassroom.service.SceneModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SceneModeServiceImpl implements SceneModeService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SceneModeMapper sceneModeMapper;

    /**
     * 查询所有场景
     * @return
     */
//    @Cacheable(value = "sceneMode",key = "'queryAllSceneMode'",unless = "#result == null ")
    @Override
    public List<SceneMode> queryAllSceneMode() {

        //创建构造器，查询置废标识为 0 的所有记录
        SceneModeExample sceneModeExample = new SceneModeExample();
        sceneModeExample.createCriteria().andDeflagEqualTo(0);
        return sceneModeMapper.selectByExample(sceneModeExample);
    }

    /**
     * 增加场景的方法
     * @param scene_id 场景编号
     * @param scene_name 场景名
     * @param number 第？个设备
     * @param deviceID 设备的编号
     * @param device_properties 设备的属性及值
     * @return
     */
//    @CacheEvict(value = {"sceneMode"},allEntries = true)
    @Override
    public int addSceneMode(Integer scene_id, String scene_name, Integer number, Integer deviceID, String device_properties) {
        SceneModeExample sceneModeExample = new SceneModeExample();
        sceneModeExample.createCriteria().andSceneIdEqualTo(scene_id).andDeflagEqualTo(0);
        SceneMode sceneMode = new SceneMode();
        sceneMode.setSceneId(scene_id);
        sceneMode.setSceneName(scene_name);
        switch (number){
            case 1:
                sceneMode.setDeviceid1(deviceID);
                sceneMode.setDeviceProperties1(device_properties);
                break;
            case 2:
                sceneMode.setDeviceid2(deviceID);
                sceneMode.setDeviceProperties2(device_properties);
                break;
            case 3:
                sceneMode.setDeviceid3(deviceID);
                sceneMode.setDeviceProperties3(device_properties);
                break;
            case 4:
                sceneMode.setDeviceid4(deviceID);
                sceneMode.setDeviceProperties4(device_properties);
                break;
            case 5:
                sceneMode.setDeviceid5(deviceID);
                sceneMode.setDeviceProperties5(device_properties);
                break;
            case 6:
                sceneMode.setDeviceid6(deviceID);
                sceneMode.setDeviceProperties6(device_properties);
                break;
            case 7:
                sceneMode.setDeviceid7(deviceID);
                sceneMode.setDeviceProperties7(device_properties);
                break;
            case 8:
                sceneMode.setDeviceid8(deviceID);
                sceneMode.setDeviceProperties8(device_properties);
                break;
            case 9:
                sceneMode.setDeviceid9(deviceID);
                sceneMode.setDeviceProperties9(device_properties);
                break;
            case 10:
                sceneMode.setDeviceid10(deviceID);
                sceneMode.setDeviceProperties10(device_properties);
                break;

        }//switch

        List<SceneMode> scene = sceneModeMapper.selectByExample(sceneModeExample);
        System.out.println(scene);
        if (scene == null) {
            return sceneModeMapper.insertSelective(sceneMode);
        } else{
            return sceneModeMapper.updateByExampleSelective(sceneMode,sceneModeExample);
        }

        /*if (scene.get(0).getSceneId().equals(scene_id)){
            return sceneModeMapper.updateByExampleSelective(sceneMode,sceneModeExample);
        }*/


    }

    /**
     * 根据场景id删除场景
     * @param scene_id
     * @return
     */
    @Override
    public int delSceneMode(Integer scene_id) {
        SceneMode sceneMode = new SceneMode();
        SceneModeExample sceneModeExample = new SceneModeExample();
        sceneModeExample.createCriteria().andDeflagEqualTo(0).andSceneIdEqualTo(scene_id);
        sceneMode.setDeflag(1);
        return sceneModeMapper.updateByExampleSelective(sceneMode,sceneModeExample);
    }

    /**
     * 根据场景id查询场景
     * @param scene_id
     * @return
     */
    @Override
    public List<SceneMode> querySceneMode(Integer scene_id) {

        SceneModeExample sceneModeExample = new SceneModeExample();
        sceneModeExample.createCriteria().andSceneIdEqualTo(scene_id).andDeflagEqualTo(0);
        return sceneModeMapper.selectByExample(sceneModeExample);
    }

}
